// 获取主表数据逻辑

import ajaxMainTableData from '../../request-functions/getMainTableData';
import deepCopy from '../../../../public/functions/deep-copy';
import { cacheTools } from 'nc-lightapp-front';
import language from '../../language';

export default function getMainTableData(cb) {
    console.log(this)
    const {
        searchModalValue,
        currentSelectDepartment,
        humanResourceOrganizationValue,
        queryOid,
        include_child_depts,
        include_child_orgs,
        pageInfo,
        show_on_job_psn
    } = this.state;

    const {
        editTable,
        button
    } = this.props;

    let postData = {
        querycondition: {
            logic: 'and',
            conditions: []
        },
        queryAreaCode: "ncc60071701query",
        querytype: "tree",
        ...searchModalValue,
        tree_node_id: currentSelectDepartment,
        node_type: "ORG_NODE",
        pk_org: humanResourceOrganizationValue.refpk,
        oid: queryOid,
        func_type: 2,
        show_all_info: false,
        show_on_job_psn: show_on_job_psn,
        include_child_depts: include_child_depts,
        include_child_orgs: include_child_orgs,
        pageInfo: pageInfo
    };

    return ajaxMainTableData(postData)
        .then((res) => {
            if (res.success) {
                if (!res.data) {
                    res.data = {
                        'ncc60071701list': {
                            rows: []
                        }
                    };
                }
                res.data['ncc60071701list'].rows.map((item) => {
                    if (item.values['showorder'].value === '9999999') {
                        item.values['showorder'].value = '';
                    }
                });
                const pks = res.data['ncc60071701list'].allpks;
                cacheTools.set('allpks', pks)
                editTable.setTableData('ncc60071701list', deepCopy(res.data['ncc60071701list']));
                editTable.focusRowByIndex('ncc60071701list', 0);
                let maxLength = Math.max(pageInfo.pageSize, res.data['ncc60071701list'].rows.length);
                maxLength = maxLength < 10 ? 10 : maxLength;
                this.setState({
                    searchModalVisible: false,
                    hasSearched: true,
                    pageInfo: res.data['ncc60071701list'].pageInfo || {
                        ...pageInfo,
                        pageIndex: 1,
                        total: 0,
                        totalPage: 1
                    },
                    exportAllpks: pks,
                    mainTableData: res.data['ncc60071701list'],
                    clickRow: {
                        index: 0,
                        record: res.data['ncc60071701list'] && res.data['ncc60071701list'].rows[0] ?
                            res.data['ncc60071701list'].rows[0] : null
                    },
                    mainTableHeight: maxLength * 30,
                    subsetCheckboxDisabled: !(res.data['ncc60071701list'] && res.data['ncc60071701list'].rows && res.data['ncc60071701list'].rows.length > 0)
                });
                this.setButtonStatus2('mainTableHasData');
                if (typeof cb === 'function') {
                    cb();
                }
            }
        });
}
